﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class SysCity
    {
        private readonly KeRui.DAL.SysCity dal = new KeRui.DAL.SysCity();

        public List<KeRui.Model.SysCity> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.SysCity> list = new List<KeRui.Model.SysCity>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.SysCity item = new KeRui.Model.SysCity();
                    item.Province = dt.Rows[i]["Province"].ToString();
                    item.City = dt.Rows[i]["City"].ToString();
                    item.District = dt.Rows[i]["District"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public List<KeRui.Model.SysCity> GetCity(string Province)
        {
            DataSet set = this.dal.GetList(Province, "", "", 2);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.SysCity> GetCityByCache(string Province)
        {
            string cacheKey = "SysCityListCity-" + Province;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetCity(Province);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.SysCity>) cache;
        }

        public static DataSet GetCitySelect()
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("SysCity", " ", "", " distinct Province,Province ");
        }

        public static DataSet GetCitySelect(string Province)
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("SysCity", " Province = '" + Province + "' and City<>''", "", " distinct City,City ");
        }

        public static DataSet GetCitySelect(string Province, string City)
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("SysCity", " Province = '" + Province + "' and City = '" + City + "' and District<>''", "", " distinct District,District ");
        }

        public List<KeRui.Model.SysCity> GetDistrict(string Province, string City)
        {
            DataSet set = this.dal.GetList(Province, City, "", 3);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.SysCity> GetDistrictByCache(string Province, string City)
        {
            string cacheKey = "SysCityListDistrict-" + Province + "-" + City;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetDistrict(Province, City);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.SysCity>) cache;
        }

        public KeRui.Model.SysCity GetModel(string Province, string City, string District)
        {
            return this.dal.GetModel(Province, City, District);
        }

        public KeRui.Model.SysCity GetModelByCache(string Province, string City, string District)
        {
            string cacheKey = "SysCityModel-" + Province + City + District;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetModel(Province, City, District);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.SysCity) cache;
        }

        public List<KeRui.Model.SysCity> GetProvince()
        {
            DataSet set = this.dal.GetList("", "", "", 1);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.SysCity> GetProvinceByCache()
        {
            string cacheKey = "SysCityListProvince-";
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetProvince();
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.SysCity>) cache;
        }
    }
}

